package bb.lanxing.calc.calculator;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.App;
import bb.lanxing.calc.data.GpsPoint;
import bb.lanxing.calc.data.SourcePoint;
import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.model.database.PacePoint;
import bb.lanxing.model.database.Workout;
import bb.lanxing.util.Log;

public class PaceCalc {
    final static private String TAG = "PaceCalc";
    private double eachKMMaxSpeed;
    private GpsPoint lastGpsPoint;
    private long lastPaceTotalDuration;
    private int paceIndex = 0;
    private boolean isGoToHalfMarathon = false;
    private boolean isGoToFullMarathon = false;
    private long lastSegmentDuration = 0;
    private double lastSegmentDistance = Utils.DOUBLE_EPSILON;

    public void initEachInfo(Workout lastWorkout) {
        lastWorkout.checkPacePoint();
        String uuid = lastWorkout.getUuid();
        PacePoint queryLastPacePoint = WorkoutDatabaseHelper.queryLastPacePoint(uuid);
        if (queryLastPacePoint != null) {
            this.isGoToHalfMarathon = WorkoutDatabaseHelper.hasHalfMarathonPoint(uuid);
            this.isGoToFullMarathon = WorkoutDatabaseHelper.hasFullMarathonPoint(uuid);
            if (App.getContext().isDebugMode()) {
                Log.v(TAG, "initEachInfo, pacePoint.getIndex() = " + queryLastPacePoint.getIndex() + ", pacePoint.getType() = "
                        + queryLastPacePoint.getType() + ", pacePoint.getDistance() = " + queryLastPacePoint.getDistance());
                StringBuilder sb = new StringBuilder();
                sb.append("initEachInfo, isGoToHalfMarathon = ");
                sb.append(this.isGoToHalfMarathon);
                sb.append(", isGoToFullMarathon = ");
                sb.append(this.isGoToFullMarathon);
                Log.v(TAG, sb.toString());
            }
            if (queryLastPacePoint.getType() == PacePoint.TYPE_LAST_ODD) {
                if (queryLastPacePoint.getDistance() < PacePoint.DISTANCE_1_KM) {
                    this.paceIndex = (int) (lastWorkout.getDistance() / PacePoint.DISTANCE_1_KM);
                } else {
                    this.paceIndex = ((int) (lastWorkout.getDistance() / PacePoint.DISTANCE_1_KM)) - 1;
                }
                this.lastPaceTotalDuration = lastWorkout.getDuration() - queryLastPacePoint.getDuration();
                this.eachKMMaxSpeed = queryLastPacePoint.getMaxSpeed();
                try {
                    WorkoutDatabaseHelper.delete(queryLastPacePoint);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                this.paceIndex = (int) (lastWorkout.getDistance() / PacePoint.DISTANCE_1_KM);
                this.lastPaceTotalDuration = lastWorkout.getDuration() - queryLastPacePoint.getDuration();
                this.eachKMMaxSpeed = queryLastPacePoint.getMaxSpeed();
            }
        } else {
            this.paceIndex = (int) (lastWorkout.getDistance() / PacePoint.DISTANCE_1_KM);
            this.lastPaceTotalDuration = lastWorkout.getDuration();
            this.eachKMMaxSpeed = Utils.DOUBLE_EPSILON;
        }
        if (App.getContext().isDebugMode()) {
            Log.v(TAG, "initEachInfo, paceIndex = " + this.paceIndex);
        }
    }

    public synchronized void calcEachKMInfo(Workout workout, SourcePoint sourcePoint) {
        long j = 0;
        if (sourcePoint != null) {
            if (sourcePoint.getGpsPoint() != null) {
                GpsPoint gpsPoint = sourcePoint.getGpsPoint();
                double distance = workout.getDistance() - (this.paceIndex * PacePoint.DISTANCE_1_KM);
                long duration = workout.getDuration() - this.lastPaceTotalDuration;
                this.eachKMMaxSpeed = Math.max(this.eachKMMaxSpeed, sourcePoint.getSpeed());
                if ((!this.isGoToHalfMarathon && workout.getDistance() >= PacePoint.DISTANCE_HALF_MARATHON)
                        || (!this.isGoToFullMarathon && workout.getDistance() >= PacePoint.DISTANCE_FULL_MARATHON)) {
                    if (App.getContext().isDebugMode()) {
                        Log.v(TAG, "calcEachKMInfo, paceIndex = " + this.paceIndex + ", isGoToHalfMarathon = " + this.isGoToHalfMarathon
                                + ", isGoToFullMarathon = " + this.isGoToFullMarathon + ", workout.getDistance() = " + workout.getDistance()
                                + ", segmentDistance = " + distance + ", eachKMMaxSpeed = " + this.eachKMMaxSpeed);
                    }
                    PacePoint pacePoint = new PacePoint();
                    pacePoint.setWorkoutUuid(workout.getUuid());
                    pacePoint.setIndex(this.paceIndex);
                    pacePoint.setSport(workout.getSport());
                    if (!this.isGoToHalfMarathon && workout.getDistance() >= PacePoint.DISTANCE_HALF_MARATHON) {
                        this.isGoToHalfMarathon = true;
                        pacePoint.setType(PacePoint.TYPE_HALF_MARATHON);
                    } else if (!this.isGoToFullMarathon && workout.getDistance() >= PacePoint.DISTANCE_FULL_MARATHON) {
                        this.isGoToFullMarathon = true;
                        pacePoint.setType(PacePoint.TYPE_FULL_MARATHON);
                    }
                    pacePoint.setLatitude(gpsPoint.getLatitude());
                    pacePoint.setLongitude(gpsPoint.getLongitude());
                    pacePoint.setAltitude(gpsPoint.getAltitude());
                    pacePoint.setTimestamp(gpsPoint.getTimestamp());
                    pacePoint.setDistance(distance);
                    pacePoint.setDuration(duration);
                    pacePoint.setMaxSpeed(this.eachKMMaxSpeed);
                    WorkoutDatabaseHelper.save(pacePoint);
                }
                while (this.lastGpsPoint != null) {
                    double d = this.lastSegmentDistance;
                    if (distance - d <= PacePoint.DISTANCE_1_KM) {
                        break;
                    }
                    double d2 = (PacePoint.DISTANCE_1_KM - d) / distance;
                    GpsPoint gpsPoint2 = new GpsPoint();
                    gpsPoint2.setSource(gpsPoint.getSource());
                    gpsPoint2.setBearing(gpsPoint.getBearing());
                    gpsPoint2.setAccuracy(gpsPoint.getAccuracy());
                    double d3 = 1.0d - d2;
                    gpsPoint2.setLatitude((this.lastGpsPoint.getLatitude() * d3) + (gpsPoint.getLatitude() * d2));
                    gpsPoint2.setLongitude((this.lastGpsPoint.getLongitude() * d3) + (gpsPoint.getLongitude() * d2));
                    gpsPoint2.setAltitude((this.lastGpsPoint.getAltitude() * d3) + (gpsPoint.getAltitude() * d2));
                    gpsPoint2.setTimestamp((long) ((d3 * this.lastGpsPoint.getTimestamp()) + (gpsPoint.getTimestamp() * d2)));
                    PacePoint pacePoint2 = new PacePoint();
                    pacePoint2.setWorkoutUuid(workout.getUuid());
                    pacePoint2.setIndex(this.paceIndex);
                    pacePoint2.setSport(workout.getSport());
                    pacePoint2.setType(PacePoint.TYPE_EACH_KM);
                    pacePoint2.setLatitude(gpsPoint2.getLatitude());
                    pacePoint2.setLongitude(gpsPoint2.getLongitude());
                    pacePoint2.setAltitude(gpsPoint2.getAltitude());
                    pacePoint2.setTimestamp(gpsPoint2.getTimestamp());
                    pacePoint2.setDistance(PacePoint.DISTANCE_1_KM);
                    pacePoint2.setDuration(this.lastSegmentDuration + ((long) ((duration - j) * d2)));
                    pacePoint2.setMaxSpeed(pacePoint2.getDistance() / pacePoint2.getDuration());
                    pacePoint2.save();
                    this.lastPaceTotalDuration += pacePoint2.getDuration();
                    distance -= PacePoint.DISTANCE_1_KM;
                    duration -= pacePoint2.getDuration();
                    this.lastGpsPoint = gpsPoint2;
                    this.lastSegmentDistance = Utils.DOUBLE_EPSILON;
                    this.lastSegmentDuration = 0L;
                    this.paceIndex++;
                }
                if (distance >= PacePoint.DISTANCE_1_KM || workout.getWorkStatus() == Workout.STATUS_STOPED) {//32
                    if (App.getContext().isDebugMode()) {
                        Log.v(TAG, "calcEachKMInfo, paceIndex = " + this.paceIndex +
                                ", segmentDistance = " + distance + ", eachKMMaxSpeed = " +
                                this.eachKMMaxSpeed + ", workout.getWorkStatus() = " + workout.getWorkStatus());
                    }
                    PacePoint pacePoint3 = new PacePoint();
                    pacePoint3.setWorkoutUuid(workout.getUuid());
                    pacePoint3.setIndex(this.paceIndex);
                    pacePoint3.setSport(workout.getSport());
                    if (workout.getWorkStatus() == Workout.STATUS_STOPED) {//32
                        pacePoint3.setType(PacePoint.TYPE_LAST_ODD);
                    } else {
                        pacePoint3.setType(PacePoint.TYPE_EACH_KM);
                    }
                    pacePoint3.setLatitude(gpsPoint.getLatitude());
                    pacePoint3.setLongitude(gpsPoint.getLongitude());
                    pacePoint3.setAltitude(gpsPoint.getAltitude());
                    pacePoint3.setTimestamp(gpsPoint.getTimestamp());
                    pacePoint3.setDistance(distance);
                    pacePoint3.setDuration(duration);
                    pacePoint3.setMaxSpeed(this.eachKMMaxSpeed);
                    WorkoutDatabaseHelper.save(pacePoint3);
                    this.lastPaceTotalDuration = workout.getDuration();
                    this.eachKMMaxSpeed = Utils.DOUBLE_EPSILON;
                    this.paceIndex++;
                }
                this.lastGpsPoint = gpsPoint;
                this.lastSegmentDistance = distance;
                this.lastSegmentDuration = duration;
            }
        }
    }

    public void reset() {
        this.paceIndex = 0;
        this.lastPaceTotalDuration = 0L;
        this.eachKMMaxSpeed = Utils.DOUBLE_EPSILON;
        this.isGoToHalfMarathon = false;
        this.isGoToFullMarathon = false;
    }
}
